import { _decorator, Component, instantiate, log, Node, Prefab } from 'cc';
import { PrefsManager } from '../AshFramework/Managers/PrefsManager';
import { WEAPON } from '../AshFramework/Datas/Constant';
import { BundleManager, Bundles } from '../AshFramework/Managers/BundleManager';
import { ZTool } from '../AshFramework/Utils/ZTool';
import { Item } from './Item';
import { GameData } from './GameData';
import { Gun } from './Gun';
import { Magazine } from './Magazine';
import { Bullet } from './Bullet';
import { TipsController } from './TipsController';
const { ccclass, property } = _decorator;

@ccclass('Backpack')
export class Backpack extends Component {
    public static Instance: Backpack = null;

    @property(Node)
    WeaponContent: Node = null;

    @property(Node)
    MagazineContent: Node = null;

    @property(Node)
    BulletContent: Node = null;

    @property(Node)
    BackButton: Node = null;

    CheckedBullet: Item = null;//选用子弹

    private _weaponName: string = "";
    private _haveMagazine: boolean = false;
    private _weapon: Gun = null;
    private _isCheckMagazine: boolean = false;

    protected onLoad(): void {
        Backpack.Instance = this;
    }


    protected onEnable(): void {
        this.showWeapon();
        if (TipsController.Instance.IsPack && TipsController.Instance.IsPackStart) {
            this.BackButton.active = false;
        }
    }

    showWeapon() {
        this.WeaponContent.removeAllChildren();
        this.CheckedBullet = null;
        const weapons: WEAPON[] = PrefsManager.Instance.userData.HaveWeapon;

        for (let i = 0; i < weapons.length; i++) {
            const name: string = ZTool.GetEnumKeyByValue(WEAPON, weapons[i]);
            const path: string = "背包_武器_" + name;
            BundleManager.LoadPrefab(Bundles.Prefabs, path).then((prefab: Prefab) => {
                const node: Node = instantiate(prefab);
                node.parent = this.WeaponContent;
                node.getComponent(Item).Name = name;
                if (i == 0) {
                    this._weaponName = name;
                    this._haveMagazine = node.getComponent(Item).HaveMagazine;
                    node.getComponent(Item).show(true);
                    this.showData();
                }
            })
        }
    }

    showData() {
        this.MagazineContent.removeAllChildren();
        if (!this._haveMagazine) {
            BundleManager.LoadPrefab(Bundles.Prefabs, "背包_弹匣包_空").then((prefab: Prefab) => {
                const node: Node = instantiate(prefab);
                node.parent = this.MagazineContent;
            });
        } else {
            this.showMazanie();
        }

        this.showBullet();
    }

    showMazanie() {
        this._weapon = GameData.getWeaponByName(this._weaponName) as Gun;
        const path: string = "背包_弹匣包_" + this._weaponName;
        this._weapon.HaveMagazine.forEach(e => {
            BundleManager.LoadPrefab(Bundles.Prefabs, path).then((prefab: Prefab) => {
                const node: Node = instantiate(prefab);
                node.parent = this.MagazineContent;
                if (!this._isCheckMagazine && e.Bullets.length < e.Capacity) {
                    this._isCheckMagazine = true;
                    node.getComponent(Item).showMagazine();
                }
                // node.getComponent(Item).showMagazine(e.Bullets.length, e.Capacity);
                node.getComponent(Item).addMagazine(e);
            });
        })
        BundleManager.LoadPrefab(Bundles.Prefabs, "背包_弹匣包_新建").then((prefab: Prefab) => {
            const node: Node = instantiate(prefab);
            node.parent = this.MagazineContent;
        });
    }

    showBullet() {
        this.BulletContent.removeAllChildren();
        const bullets: Bullet[] = GameData.getBulletByWeapon(WEAPON[this._weaponName]);
        if (bullets.length == 0) {
            this.CheckedBullet = null;
            BundleManager.LoadPrefab(Bundles.Prefabs, "背包_子弹包_空").then((prefab: Prefab) => {
                const node: Node = instantiate(prefab);
                node.parent = this.BulletContent;
            });
        } else {
            for (let i = 0; i < bullets.length; i++) {
                const bullet: Bullet = bullets[i];
                BundleManager.LoadPrefab(Bundles.Prefabs, "背包_子弹包_" + bullet.Name).then((prefab: Prefab) => {
                    const node: Node = instantiate(prefab);
                    node.parent = this.BulletContent;
                    node.getComponent(Item).addBulletTs(bullet);
                    if (i == 0 && this._isCheckMagazine) {
                        this.CheckedBullet = node.getComponent(Item);
                        node.getComponent(Item).show(true);
                    }
                });
            }
        }
    }

    switchWeapon(weaponItem: Item) {
        this._isCheckMagazine = false;
        this._weaponName = weaponItem.Name;
        this._haveMagazine = weaponItem.HaveMagazine;
        weaponItem.show(true);
        this.showData();
    }

    newMagazine() {
        this._isCheckMagazine = false;
        this._weapon.HaveMagazine.push(new Magazine(this._weapon.Capacity));
        this.showData();
        GameData.DateSave();
    }

}


